package com.cn.http.utils;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * &#064;Time 2024 八月 星期五 15:57
 *
 * @author ShangGuan
 */
public class CollectionUtils {

    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null)&& collection.isEmpty();
    }

    // 将List<Map<String, Object>>转换为URL查询参数字符串
    public static String listMapToUrlParams(List<Map<String, Object>> list) throws UnsupportedEncodingException {
        StringBuilder urlParams = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            urlParams.append(MapUtils.mapToUrlParams(map));
            if (i < list.size() - 1) {
                urlParams.append("&");
            }
        }
        return urlParams.toString();
    }



    public static String listMapToJsonStr(List<Map<String, Object>> list) {
        List<Map<Object, Object>> result = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            stringObjectMap.forEach((key, value) -> {
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put(key, value);
                result.add(hashMap);
            });
        }
        return listMapToJson(result);
    }

    // 方法将List<Map<String, Object>>转换为JSON字符串
    public static String listMapToJson(List<Map<Object, Object>> list) {
        StringBuilder json = new StringBuilder();
        json.append("[");
        for (int i = 0; i < list.size(); i++) {
            Map<Object, Object> map = list.get(i);
            json.append(MapUtils.mapToJson(map));
            if (i < list.size() - 1) {
                json.append(",");
            }
        }
        json.append("]");
        return json.toString();
    }


    public static void checkObjectType(Object target, StringBuilder stringBuilder) {
        if (target instanceof String) {
            stringBuilder.append("\"").append(target).append("\"");
        } else if (target instanceof Number || target instanceof Boolean) {
            stringBuilder.append(target);
        } else if (target instanceof Map) {
            stringBuilder.append(MapUtils.mapToJson((Map<Object, Object>) target));
        } else if (target instanceof List<?>) {
            stringBuilder.append(listToJson((List<Object>) target));
        } else {
            stringBuilder.append("\"").append(target.toString()).append("\"");
        }
    }

    // 方法将List<Object>转换为JSON字符串
    public static String listToJson(List<Object> list) {
        StringBuilder json = new StringBuilder();
        json.append("[");
        for (int i = 0; i < list.size(); i++) {
            Object element = list.get(i);
            checkObjectType(element, json);
            if (i < list.size() - 1) {
                json.append(",");
            }
        }
        json.append("]");
        return json.toString();
    }


    public static void main(String[] args) {
        Map<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(1, "asd");
        ArrayList<Map<Object, Object>> objects = new ArrayList<>();
        objects.add(objectObjectHashMap);
        System.out.println(listMapToJson(objects));
    }
}
